<?php

namespace app\model\v1\gift;

use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Model;


class GiftCardEffect extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    protected $table = 'eb_gift_card_effect';



    public function cate()
    {
        return $this->hasOne(GiftCardCate::class,'id','cate_id');
    }



    /**
     * 礼品卡后台 - 特效管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getGiftEffectList($param = [])
    {
        $data["data"] = self::field ("*,FROM_UNIXTIME(create_time) as create_time")
            ->with(['cate'=> function($query) {
                $query->field('*');
            }])
            ->whereNull("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["cate_id"])) $query->where("cate_id",  $param["cate_id"]);
            })
            ->order("id", "desc")
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->whereNull("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["cate_id"])) $query->where("cate_id",  $param["cate_id"]);
            })
            ->count();

        return $data;
    }



    public static function getGiftCardCateData($param = [])
    {
        return self::field ("id,cate_tag_name")
            ->whereNull("delete_time")
            ->where("status", 1)
            ->select()->toArray();
    }


    /**
     * 特效管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addGiftEffect($param = [])
    {
        $add = [
            "cate_id"       => $param["cate_id"],
            "file_url"      => $param["file_url"] ?? '',
            "cover_url"     => $param["cover_url"] ?? '',
            "type"          => $param["type"] ?? 1,
            "sort"          => $param["sort"] ?? '',
            "create_time"   => time(),
        ];
        return self::insert($add);
    }


    /**
     * 特效管理列表 - 编辑
     * @param $param
     * @return RecruitLanguage
     */
    public static function updateGiftEffect($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["cate_id"]))       $update["cate_id"] = $param["cate_id"];
        if(!empty($param["type"]))          $update["type"] = $param["type"];
        if(!empty($param["sort"]))          $update["sort"] = $param["sort"]; //排序
        if(!empty($param["file_url"]))      $update["file_url"] = $param["file_url"]; //icon
        if(!empty($param["cover_url"]))     $update["cover_url"] = $param["cover_url"]; //icon

        return self::where("id", $param["id"])->update($update);
    }


    /**
     * 特效管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableGiftEffect($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }




}